package com.fitness.fitness.controller;

import com.fitness.fitness.modal.response.ServerResponseEntity;
import com.fitness.fitness.service.impl.common.UploadFileServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/common")
public class UploadFileController {


    private final UploadFileServiceImpl uploadFileService;

    //    @SaCheckLogin
    @PostMapping("/upload")
    public ServerResponseEntity<String> upload(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();  // 文件名
        String url = uploadFileService.uploadFile(file.getInputStream(),fileName);
        return ServerResponseEntity.success(url);
    }

    @PostMapping("/multipleUpload")
    public ServerResponseEntity<List<String>> multipleUpload(MultipartFile[] files, HttpRequest request) throws Exception {
        List<String> urls = new ArrayList<>();
        for(MultipartFile file : files){
            String fileName = file.getOriginalFilename();  // 文件名
            String url = uploadFileService.uploadFile(file.getInputStream(),fileName);
            urls.add(url);
        }

        return ServerResponseEntity.success(urls);
    }

}
